package mil.af.amc.adpetracker.web.actions.AdpeAccount;

import com.opensymphony.xwork2.ActionSupport;
import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.model.DRA;
import mil.af.amc.adpetracker.service.AdpeAccountService;
import mil.af.amc.adpetracker.service.DraService;
import mil.af.amc.adpetracker.service.ServiceException;
import mil.af.amc.adpetracker.service.exceptions.DraNotFoundException;
import mil.af.amc.adpetracker.exceptions.IllegalUnassignmentOfPrimaryCustodianException;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * AdpeAccountAcount.java
 *
 * This file is a reimplementation of the older AdpeAccount-related Struts 1.X actions into Struts 2 format
 *
 * Here is the TO-DO list for this mess of code
 * 1. Implement assignPrimaryCustodian()
 * 2. Implement assignAlternateCustodian()
 * 3. Implement removeAlternateCustodian()
 * 4. Implement index()
 * 5. UNIT TESTS!
 *
 * @author TSgt Jason Ferguson
 */
public class AdpeAccountAction extends ActionSupport implements ServletRequestAware {

    // autowired services

    private AdpeAccountService adpeAccountService;

    @Autowired
    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    private DraService draService;

    @Autowired
    public void setDraService(DraService draService) {
        this.draService = draService;
    }

    private HttpServletRequest request;

    @Autowired
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    // form fields - fields on the jsp must match the names of the fields listed here

    private int draId;
    private int adpeAccountId;
    private String accountName;
    private List<DRA> draList;
    private Integer[] alternameCustodianUserId;

    public void setDraId(int draId) {
        this.draId = draId;
    }

    public void setAdpeAccountId(int adpeAccountId) {
        this.adpeAccountId = adpeAccountId;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public void setDraList(List<DRA> draList) {
        this.draList = draList;
    }

    public void setAlternameCustodianUserId(Integer[] alternameCustodianUserId) {
        this.alternameCustodianUserId = alternameCustodianUserId;
    }

    // action methods

    public String populate() {

        draList = draService.getAll();
        
        return "populate";
    }
    
    public String create() throws Exception {

        if (request.getParameter("accountName") == null) {
            return INPUT;
        }

        try {
            AdpeAccount acct = adpeAccountService.createAccount(draId, accountName);

            return SUCCESS;
        } catch (DraNotFoundException e) {
            addActionMessage(e.getMessage());

            return INPUT;
        }
    }

    public String delete() throws Exception {

        if (request.getParameter("id")==null) {
            return INPUT;
        }

        int id = Integer.parseInt(request.getParameter("id"));
        adpeAccountService.deleteAccount(id);

        return SUCCESS;

    }

    public String assignAlternateCustodian() throws Exception {

        // Perform a check to see if we should be performing data entry or the actual job. If there is no
        // adpe account id in the request parameter, we need to display the input form
        if (request.getParameter("adpeAccountId") == null) {
            return INPUT;
        }

        // Since we have data, process it
        try {
            // assign the alternate custodian
            adpeAccountService.assignAlternateCustodian(adpeAccountId, alternameCustodianUserId);
            return SUCCESS;
        } catch (ServiceException e) {

            addActionMessage(e.getMessage());
            return INPUT;

        } catch (IllegalUnassignmentOfPrimaryCustodianException e) {

            addActionMessage(e.getMessage());
            return INPUT;

        }


    }


}
